package com.easy.user.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.easy.common.bean.SysPermission;
import com.easy.common.service.IPermissionService;
import com.easy.common.util.ResultData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("permission")
public class PermissionController {

    @Autowired
    private IPermissionService permissionService;

    // 插入权限
    @PostMapping("/")
    public ResultData insert(@RequestBody SysPermission sysPermission) {
        SysPermission data = permissionService.insert(sysPermission);
        return ResultData.success(data);
    }

    // 更新权限
    @PutMapping("/")
    public ResultData update(@RequestBody SysPermission sysPermission) {
        SysPermission data = permissionService.update(sysPermission);
        return ResultData.success(data);
    }

    // 删除权限
    @DeleteMapping("/{id}")
    public ResultData delete(@PathVariable Integer id) {
        int result = permissionService.delete(id);
        if (result >= 1) {
            return ResultData.success("200", "删除成功", null);
        } else {
            return ResultData.fail();
        }
    }

    // 根据 ID 获取权限
    @GetMapping("/{id}")
    public ResultData getById(@PathVariable Integer id) {
        SysPermission data = permissionService.getById(id);
        return ResultData.success(data);
    }

    // 分页获取权限列表
    @GetMapping("/page")
    public ResultData getListByPage(Page page, SysPermission sysPermission) {
        IPage<SysPermission> data = permissionService.getListByPage(page, sysPermission);
        return ResultData.success(data);
    }

    // 获取权限列表
    @GetMapping("/list")
    public ResultData getList(SysPermission sysPermission) {
        List<SysPermission> data = permissionService.getList(sysPermission);
        return ResultData.success(data);
    }
}